<?php
function getItemCount($itemID)
{
	$tables = array();
	$tables[0] = 'inv_qtyIndex';
	
	$fields = array();
	$fields[0] = 'quantity';
	
	$whereClause = 'itemID = "'.$itemID.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	$qtyTotal = 0;
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		for ($i=0; $i<sizeOf($data['itemID']); $i++)
		{
			$qtyTotal += $data['itemID'][$i];		
		}
	}
	return $qtyTotal;
}
function getDeletedCatID()
{
	$tables = array();
	$tables[0] = 'inv_category';
	
	$fields = array();
	$fields[0] = 'catID';
	
	$whereClause = 'catName = "Deleted"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	return $data['catID'][0];
}
function installDeleteCategory()
{
	$check = checkIfExists('inv_category', 'Deleted', 'catName', 'checker');
	if($check != 'DB Query success!')
	{
		$tableName = 'inv_category';
		
		$fields = array();
		$fields[0] = 'catName';
		
		$values = array();
		$values[0] = 'Deleted';
				
		$data = SQLInsert($tableName, $fields, $values);
		debugRunTime('Delete feature: Installed.');
	}
	
	$deleted = checkIfExists('inv_category', 'Deleted', 'catName', 'catID');
	
	$tables = array();
	$tables[0] = 'inv_item';
	
	$fields = array();
	$fields[0] = 'itemID';
	
	$whereClause = 'active="1" OR category="'.$deleted.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	
	if($data['queryStatus'] == 'DB Query success!')
	{
		for ($i=0; $i<sizeOf($data['itemID']); $i++)
		{
			$tableNames = 'inv_item';
		
			$fieldss = array();
			$fieldss[0] = 'category';
			$fieldss[1] = 'active';
			
			$valuess = array();
			$valuess[0] = $deleted;
			$valuess[1] = '1';
			
			$whereClauses = 'itemID="'.$data['itemID'][$i].'"';
			
			$datas = SQLUpdate($tableNames, $whereClauses, $fieldss, $valuess);
		}
	}
}

function getDeletedCategory()
{
	$deleted = checkIfExists('inv_category', 'Deleted', 'catName', 'catID');
	return '<option value="'.$deleted.'">*Mark as Deleted</option>';
}

function recoverItemList($category)
{
	//if category == all, return all, else, return per category.
	$tables = array();
	$tables[0] = 'inv_item';
	
	$fields = array();
	$fields[0] = '*';
	$whereClause = 'active = "0"';
	$whereClause .= ($category == 'all' ? '' : ' AND category = "'.$category.'"');

	$data = SQLRecover($tables, $fields, $whereClause);
	return $data;
}

function recoverDeletedItems()
{
	$tables = array();
	$tables[0] = 'inv_item';
	
	$fields = array();
	$fields[0] = '*';
	$whereClause = 'active = "1"';

	$data = SQLRecover($tables, $fields, $whereClause);
	return $data;
}

function iterateFields($openTag, $closeTag, $field, $dbResponse)
{
	$display = '';
	for ($i=0; $i<sizeOf($dbResponse[$field]); $i++)
	{
		$display .= $openTag.$dbResponse[$field][$i].$closeTag;
	}
	return $display;
}

function iterateTwoColumnedTable($field, $dbResponse)
{
	$display = '';
	$end = 0;
	for ($i=0; $i<sizeOf($dbResponse[$field]); $i++)
	{
		$j = $i%2;
		if($j == 0)
		{
			$display .= '<tr>';
			$display .= '<td>'.$dbResponse[$field][$i].'</td>';
		}
		
		if($j == 1)
		{
			$display .= '<td>'.$dbResponse[$field][$i].'</td>';
			$display .= '</tr>';
		}
		$end = $j;
	}
	if($end == 0)
	{
		$display .= '</tr>';
	}
	return $display;
}


function recoverItemVarieties($category)
{
	$tables = array();
	$tables[0] = '';
	
	$fields = array();
	$fields[0] = '';
	
	$whereClause = '';
	
	$data = SQLDistinct($tables, $fields, $whereClause);
}

function countItems($category)
{
	$tables = array();
	$tables[0] = 'inv_item';
	
	$whereClause = 'category="'.$category.'";';
	
	$data = SQLCount($tables, $whereClause);
	return $data['COUNT(*)'][0];
}

function recoverCategoryInfo($id)
{
	$tables = array();
	$tables[0] = 'inv_category';
	
	$fields = array();
	$fields[0] = '*';
	
	$whereClause = 'catID="'.$id.'"';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	return $data;
}

function recoverItemInfo($id)
{
	$tables = array();
	$tables[0] = 'inv_item';
	
	$fields = array();
	$fields[0] = '*';
	
	$whereClause = 'itemID="'.$id.'";';
	
	$data = SQLRecover($tables, $fields, $whereClause);
	if($data['queryStatus'] == 'DB Query success!')
	{
		$tables1 = array();
		$tables1[0] = 'inv_category';
		
		$fields1 = array();
		$fields1[0] = 'catName';
		
		$whereClause1 = 'catID="'.$data['category'][0].'"';
		
		$category = SQLRecover($tables1, $fields1, $whereClause1);
		$data['catName'] = $category['catName'];

	}
	
	return $data;
}


function recoverCategories()
{
	$tables = array();
	$tables[0] = 'inv_category';
	
	$fields = array();
	$fields[0] = '*';
	$whereClause = 'catName != "Deleted"';
		
	$data = SQLRecover($tables, $fields, $whereClause);
	
	return $data;
}

function recoverLocationList()
{
	$tables = array();
	$tables[0] = 'inv_location';
	
	$fields = array();
	$fields[0] = 'locName';
	$fields[1] = 'locDesc';
	
	$whereClause = 'none';
	
	$data = SQLRecover($tables, $fields, $whereClause);

	return $data;
}

function changeToOptions($value, $text, $getVar)
{
	if(sizeof($value) != sizeof($text))
	{
		return '<option>Value Count Mismatch Error.</option>';
	}
	else
	{
		$output = '';
		for ($i=0; $i<sizeOf($value); $i++)
		{
			if($value[$i] == $getVar)
			{
				$output.= '<option value="'.$value[$i].'" selected>'.$text[$i].'</option>';
			}
			else
			{
				$output.= '<option value="'.$value[$i].'">'.$text[$i].'</option>';
			}
			
		}
		return $output;	
	}
}

//INSTALLATION/GARBAGE COLLECTION SCRIPT
installDeleteCategory();
?>